﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VideoStream.Model;

namespace VideoStream.CustomControl
{
    public partial class PictureButton : UserControl
    {
        #region Filds

        private bool enable = true;

        public new bool Enabled
        {
            get { return enable; }
            set
            {
                if (enable != value)
                {
                    enable = value;
                    OnEnabledChanged(EventArgs.Empty);
                }
            }
        }

        // font color
        private Color normalFgColor = TVColor.Color_Dark;
        private Color disableFgColor = TVColor.Color_Dark;
        private Color hoverFgColor = TVColor.Color_Dark;
        private Color pressFgColor = TVColor.Color_Dark;
        private Color checkedFgColor = TVColor.Color_Dark;

        // background color
        private Color normalBgColor = TVColor.Color_GrayLight;
        private Color disableBgColor = TVColor.Color_GrayLight;
        private Color hoverBgColor = TVColor.Color_GrayLight;
        private Color pressBgColor = TVColor.Color_GrayLight;
        private Color checkedBgColor = TVColor.Color_GrayLight;

        // background image
        private Image normalBgImage;
        private Image disableBgImage;
        private Image hoverBgImage;
        private Image pressBgImage;
        private Image checkedBgImage;

        // tmpUI
        private Color preBgColor;
        private Image preBgImage;
        private Color preFgColor;

        // status
        private bool _isNormal = true;
        private bool _isHover = false;
        private bool _isPressed = false;
        private bool _isChecked = false;

        private bool isNormal
        {
            get { return _isNormal; }
            set
            {
                _isNormal = value;
                if (isNormal)
                {
                    isHover = false;
                    isPressed = false;
                    isChecked = false;
                }
            }
        }
        private bool isHover
        {
            get { return _isHover; }
            set
            {
                _isHover = value;
                if (isHover)
                {
                    isNormal = false;
                    isPressed = false;
                    isChecked = false;
                }
            }
        }
        private bool isPressed
        {
            get { return _isPressed; }
            set
            {
                _isPressed = value;
                if (isPressed)
                {
                    isHover = false;
                    isNormal = false;
                    isChecked = false;
                }
            }
        }
        private bool isChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                if (isChecked)
                {
                    isHover = false;
                    isPressed = false;
                    isNormal = false;
                }
            }
        }

        private ButtonType buttonType = CustomControl.ButtonType.Normal;

        #endregion


        public bool ShowImage
        {
            get { return pic_Image.Visible; }
            set
            {
                pic_Image.Visible = value;
                lab_Text.Dock = value ? textDock : DockStyle.Fill;
            }
        }

        public bool ShowText
        {
            get { return lab_Text.Visible; }
            set { lab_Text.Visible = value; }
        }

        // font color
        public Color NormalFgColor
        {
            get { return normalFgColor; }
            set
            {
                normalFgColor = value;
                if (this.Enabled)
                    lab_Text.ForeColor = NormalFgColor;
            }
        }
        public Color DisableFgColor
        {
            get { return disableFgColor; }
            set
            {
                disableFgColor = value;
                if (!this.Enabled)
                    lab_Text.ForeColor = DisableFgColor;
            }
        }
        public Color HoverFgColor
        {
            get { return hoverFgColor; }
            set { hoverFgColor = value; }
        }
        public Color PressFgColor
        {
            get { return pressFgColor; }
            set { pressFgColor = value; }
        }
        public Color CheckedFgColor
        {
            get { return checkedFgColor; }
            set
            {
                checkedFgColor = value;
                if (Checked)
                    lab_Text.ForeColor = CheckedFgColor;
            }

        }

        // background color
        public Color NormalBgColor
        {
            get { return normalBgColor; }
            set
            {
                normalBgColor = value;
                if (this.Enabled)
                    this.BackColor = NormalBgColor;
            }
        }
        public Color DisableBgColor
        {
            get { return disableBgColor; }
            set
            {
                disableBgColor = value;
                if (!this.Enabled)
                    this.BackColor = DisableBgColor;
            }
        }
        public Color HoverBgColor
        {
            get { return hoverBgColor; }
            set { hoverBgColor = value; }
        }
        public Color PressBgColor
        {
            get { return pressBgColor; }
            set { pressBgColor = value; }
        }
        public Color CheckedBgColor
        {
            get { return checkedBgColor; }
            set
            {
                checkedBgColor = value;
                if (Checked)
                    this.BackColor = CheckedBgColor;
            }
        }
        // background image
        public Image NormalBgImage
        {
            get { return normalBgImage; }
            set
            {
                normalBgImage = value;
                if (this.Enabled)
                    pic_Image.BackgroundImage = NormalBgImage;
            }
        }
        public Image DisableBgImage
        {
            get { return disableBgImage; }
            set
            {
                disableBgImage = value;
                if (!this.Enabled)
                    pic_Image.BackgroundImage = DisableBgImage;
            }
        }
        public Image HoverBgImage
        {
            get { return hoverBgImage; }
            set { hoverBgImage = value; }
        }
        public Image PressBgImage
        {
            get { return pressBgImage; }
            set { pressBgImage = value; }
        }
        public Image CheckedBgImage
        {
            get { return checkedBgImage; }
            set
            {
                checkedBgImage = value;
                if (Checked)
                    pic_Image.BackgroundImage = CheckedBgImage;
            }
        }



        public ImageLayout ImageLayout
        {
            get { return pic_Image.BackgroundImageLayout; }
            set { pic_Image.BackgroundImageLayout = value; }
        }

        public string _Text
        {
            get { return lab_Text.Text; }
            set { lab_Text.Text = value; }
        }

        public ContentAlignment TextAlign
        {
            get { return lab_Text.TextAlign; }
            set { lab_Text.TextAlign = value; }
        }

        public Font TextFontSize
        {
            get { return lab_Text.Font; }
            set { lab_Text.Font = value; }
        }

        public DockLayout DockLayout
        {
            get { return dockLayout; }
            set
            {
                dockLayout = value;
                switch (DockLayout)
                {
                    case DockLayout.TB:
                        imgDock = DockStyle.Fill;
                        textDock = DockStyle.Bottom;
                        break;
                    case DockLayout.LR:
                        imgDock = DockStyle.Fill;
                        textDock = DockStyle.Right;
                        break;
                    case DockLayout.BT:
                        imgDock = DockStyle.Fill;
                        textDock = DockStyle.Top;
                        break;
                    case DockLayout.RL:
                        imgDock = DockStyle.Fill;
                        textDock = DockStyle.Left;
                        break;
                    case DockLayout.O:
                        imgDock = DockStyle.Fill;
                        textDock = DockStyle.None;
                        break;
                    default:
                        break;
                }
                lab_Text.Dock = textDock;
                if (lab_Text.Dock == DockStyle.None)
                {
                    lab_Text.Anchor = AnchorStyles.None;
                    lab_Text.BringToFront();
                    lab_Text.Location = new Point((this.Width - lab_Text.Width) / 2, (this.Height - lab_Text.Height) / 2);
                }
                else
                {
                    lab_Text.SendToBack();
                }
                pic_Image.Dock = imgDock;

            }
        }

        public Size TextWidth
        {
            get
            {
                return lab_Text.Size;
            }
            set
            {
                lab_Text.Size = value;
            }
        }

        public ButtonType ButtonType { get { return buttonType; } set { buttonType = value; } }
        private DockStyle textDock;
        private DockStyle imgDock;
        private DockLayout dockLayout;
        private Size BGImageSize = Size.Empty;
        private Point BGImagePoint = Point.Empty;

        private bool _checked = false;

        public bool Checked
        {
            get { return _checked; }
            set
            {
                _checked = value;
                if (Checked)
                {
                    SetCheckedUI();
                }
                else
                {
                    SetNormalUI();
                }
            }
        }
        public PictureButton()
        {
            InitializeComponent();

            pic_Image.MouseEnter += PictureButton_MouseEnter;
            pic_Image.MouseLeave += PictureButton_MouseLeave;
            pic_Image.MouseDown += PictureButton_MouseDown;
            pic_Image.MouseUp += PictureButton_MouseUp;
            lab_Text.MouseEnter += PictureButton_MouseEnter;
            lab_Text.MouseLeave += PictureButton_MouseLeave;
            lab_Text.MouseDown += PictureButton_MouseDown;
            lab_Text.MouseUp += PictureButton_MouseUp;
            pic_Image.Click += PictureButton_Click;
            lab_Text.Click += PictureButton_Click;
            //lab_Text.EnabledChanged += Lab_Text_EnabledChanged;
            DockLayout = DockLayout.TB;


            lab_Text.SizeChanged += Lab_Text_SizeChanged;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnParentEnabledChanged(EventArgs e)
        {
            base.OnParentEnabledChanged(e);
            Enabled = (bool)Parent?.Enabled;
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            if (this.Enabled)
            {
                SetNormalUI();
            }
            else
            {
                SetDisableUI();
            }

            //base.OnEnabledChanged(e);

            //if (!this.Enabled)
            //{
            //    preColor = this.BackColor;
            //    this.BackColor = DisableColor;
            //}
            //else
            //{
            //    this.BackColor = preColor;
            //}
        }

        private void Lab_Text_SizeChanged(object sender, EventArgs e)
        {
            lab_Text.Location = new Point((this.Width - lab_Text.Width) / 2, (this.Height - lab_Text.Height) / 2);
            BGImageSize.Width = this.Width - this.Padding.Left - this.Padding.Right;
            BGImageSize.Height = this.Height - this.Padding.Top - this.Padding.Bottom;
            BGImagePoint.X = this.Padding.Left;
            BGImagePoint.Y = this.Padding.Top;
        }

        private void PictureButton_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.Enabled)
            {
                if (!Checked)
                {
                    SetHoverUI();
                }
                this.OnMouseUp(e);
            }
        }

        private void PictureButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.Enabled)
            {
                switch (this.ButtonType)
                {
                    case ButtonType.Normal:
                        SetPressUI();
                        break;
                    case ButtonType.Check:
                        SetPressUI();
                        break;
                    case ButtonType.Radio:
                        if (!Checked)
                            SetPressUI();
                        break;
                    default:
                        break;
                }

                this.OnMouseDown(e);
            }
        }

        private void PictureButton_Click(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                switch (this.ButtonType)
                {
                    case ButtonType.Normal:
                        break;
                    case ButtonType.Check:
                        Checked = !Checked;
                        break;
                    case ButtonType.Radio:
                        Checked = true;
                        break;
                    default:
                        break;
                }

                this.OnClick(e);
            }
        }

        private void PictureButton_MouseEnter(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                switch (this.ButtonType)
                {
                    case ButtonType.Normal:
                        SetHoverUI();
                        break;
                    case ButtonType.Check:
                        SetHoverUI();
                        break;
                    case ButtonType.Radio:
                        if (!Checked)
                            SetHoverUI();
                        break;
                    default:
                        break;
                }
                this.OnMouseEnter(e);
            }
        }

        private void PictureButton_MouseLeave(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                SetNormalUI();
                this.OnMouseLeave(e);
            }
        }


        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);


        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);


        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);


        }


        private void SetNormalUI()
        {
            if (Checked)
            {
                SetCheckedUI();
            }
            else
            {
                this.BackColor = NormalBgColor;
                pic_Image.BackgroundImage = NormalBgImage;
                lab_Text.ForeColor = NormalFgColor;
            }
        }

        private void SetHoverUI()
        {
            this.BackColor = HoverBgColor == Color.Empty ? NormalBgColor : HoverBgColor;
            pic_Image.BackgroundImage = HoverBgImage == null ? NormalBgImage : HoverBgImage;
            lab_Text.ForeColor = HoverFgColor == Color.Empty ? NormalFgColor : HoverFgColor;
        }

        private void SetPressUI()
        {
            this.BackColor = PressBgColor == Color.Empty ? NormalBgColor : PressBgColor;
            pic_Image.BackgroundImage = PressBgImage == null ? NormalBgImage : PressBgImage;
            lab_Text.ForeColor = PressFgColor == Color.Empty ? NormalFgColor : PressFgColor;
        }

        private void SetCheckedUI()
        {
            this.BackColor = CheckedBgColor == Color.Empty ? NormalBgColor : CheckedBgColor;
            pic_Image.BackgroundImage = CheckedBgImage == null ? NormalBgImage : CheckedBgImage;
            lab_Text.ForeColor = CheckedFgColor == Color.Empty ? NormalFgColor : CheckedFgColor;
        }

        private void SetDisableUI()
        {
            this.BackColor = DisableBgColor == Color.Empty ? NormalBgColor : DisableBgColor;
            pic_Image.BackgroundImage = DisableBgImage == null ? NormalBgImage : DisableBgImage;
            lab_Text.ForeColor = DisableFgColor == Color.Empty ? NormalFgColor : DisableFgColor;
        }
    }

    public enum DockLayout
    {
        TB,
        LR,
        BT,
        RL,
        O,
    }

    public enum ButtonType
    {
        Normal,
        Check,
        Radio,
    }
}
